//
//  QKStatusListViewModel.m
//  QKWeibo-OC
//
//  Created by iMac_Kurt on 16/8/31.
//  Copyright © 2016年 Kurt_Qi. All rights reserved.
//

#import "QKStatusListViewModel.h"
#import "NetworkTool.h"
#import "QKStatus.h"

@implementation QKStatusListViewModel
/// 获取微博数据
///
/// @param finished 完成回调
- (void)loadStatusList:(void(^)(BOOL success))finished {
    [[NetworkTool sharedTools] getStatusData:^(id result, NSError *error) {
        
        if (error != nil) {
            NSLog(@"出错了");
            finished(NO);
            return;
        }
        
        // 将获取的微博数据存到数组中
        NSArray *dataList = result[@"statuses"];
        
        // 创建一个可变的数组来存放字典转化过来的模型
        self.statusList = [NSMutableArray array];
        
        // 遍历微博数组，将每一个元素转成模型字典，存入可变数组中
        for (NSDictionary *dict in dataList) {
            // 拼接数据：把最新的微博插入到微博列表的最前位置
            QKStatusViewModel *statusViewModel = [[QKStatusViewModel alloc] init];
            statusViewModel.status = [QKStatus initWithDict:dict];
            [self.statusList insertObject: statusViewModel atIndex:0];
        }
        
        NSLog(@"%@", self.statusList);
        
        // 完成回调
        finished(YES);
        
    }];

}

// 懒加载
- (NSMutableArray *)statusList {
    if (_statusList == nil) {
        _statusList = [NSMutableArray array];
    }
    return _statusList;
}

@end
